

import util from "../../common/js/util.js";

import parse from 'mini-html-parser2';
Component({
  mixins: [],
  data: {
    nodes: [
      //   {
      //   name: 'p',
      //   attrs: {
      //     class: 'wrapper',
      //     style: 'color: orange;',
      //   },
      //   children: [{
      //     type: 'text',
      //     text: 'Hello World!',
      //   }],
      // }
    ],
    img:""
  },
  props: {
    data: "",
    default_text: ""
  },
  didMount() {
    let text = this.props.data;    
    //console.log(text);
    
    this.parseHtml(text);
  },
  didUpdate() {
  },
  didUnmount() {
  },
  methods: {

    parseHtml(html) {
      parse(html, (err, nodes) => {
          if (!err) {
           console.log("----nodes:",nodes);
            this.setData({
              nodes
            })
          }
          if(err){
            console.log(err);
            
          }
      })

    },
    clickFn(e){
    //  console.log(e);
      let node = e.currentTarget.dataset.node;
      
      this.forEachImg(node);
    },
    //递归查找图片
    forEachImg(node){
    
      this.forEachGetImgUrl(node);
      let img=this.data.img;
      if(util.isNotEmpty(img)){
        dd.previewImage({
          urls: [img],
          success(r) {
            
          }
        });
      }
     // console.log("--img:",this.data.img);
    },
    forEachGetImgUrl(node){
      
      if(util.isNotEmpty(node)){
        for (let a = 0; a < node.length; a++) {
          let item = node[a];
          if(item.name=="a"&&item.attrs.href){
           let img=item.attrs.href;
           this.setData({
            img
           });
           break;
          }else{
            this.forEachGetImgUrl(item.children)
          }
        }
      }
    }
  },
});
